<template>
  <div class="login-container">
    <div class="login-left">
      <div>
        Hi,你好！
      </div>
      <div>
        欢迎登录护士上门医院管理系统
      </div>
    </div>
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
      label-position="left">
      <div class="title-container">
        <img class="title-img" src="../../../public/img/login-log.png" alt="">
        <div class="title">
          护士上门
        </div>
      </div>

      <el-form-item prop="account">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input ref="account" v-model="loginForm.account" placeholder="账号" name="account" type="text" tabindex="1"
          auto-complete="on" />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType" placeholder="密码"
          name="password" tabindex="2" auto-complete="on" @keyup.enter.native="handleLogin" />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <el-checkbox v-model="checked" style="margin-bottom: 20px;">记住密码</el-checkbox>
      <el-button :loading="loading" :style="backColor()" style="width:100%;height: 48px; margin-bottom:30px;"
        @click.native.prevent="handleLogin">登录</el-button>
      <div style="display: flex;align-items: center;margin-bottom: 20px;">
        <el-radio v-model="radio" label="1">我已阅读并同意</el-radio>
        <span style="color: #409EFF;">《用户协议》</span>
        <span>和</span>
        <span style="color: #409EFF;">《隐私政策》</span>
      </div>
      <div style="color: #409EFF;text-align: center;text-decoration:underline">忘记密码？</div>
    </el-form>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'

export default {
  name: 'Login',
  data () {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        account: '',
        password: '123456'
      },
      loginRules: {
        account: [{ required: true, trigger: 'blur', message: '请输入账号' }],
        password: [{ required: true, trigger: 'blur', message: '请输入密码' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      checked: false,
      radio: ''
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd () {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin () {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/Login', this.loginForm).then((response) => {
            localStorage.setItem('hospitalUid', response.data.uid)
            localStorage.setItem('hospitalAme', response.data.hospitalAme)
            console.log("response", response)
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          this.loading = false
          return false
        }
      })
    },
    backColor () {
      if (this.loginForm.account && this.loginForm.password) {
        return {
          background: '#409EFF',
          color: '#fff'
        }
      } else {
        return {
          background: '#e2e2e2'
        }
      }
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {


  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    //background: #fff;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #000;
      height: 47px;
      caret-color: #666;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid #E1E1E1;
    background: #fff;
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  display: flex;
  min-height: 100%;
  width: 100%;
  background-image: url('../../../public/img/login-bg.png');
  background-size: 100% 100%;
  overflow: hidden;

  .login-left {
    width: 1000px;
    font-size: 60px;
    color: #fff;
    line-height: 90px;
    padding: 360px 0 0 124px;
    letter-spacing: 2px;
  }

  .login-form {
    position: relative;
    top: 250px;
    width: 520px;
    height: 540px;
    max-width: 100%;
    background: #fff;
    border-radius: 10px;
    padding: 60px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 10px;
    margin-bottom: 60px;

    .title-img {
      width: 50px;
      height: 50px;
    }

    .title {
      font-size: 32px;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
